layui.define(['table', 'form', 'laypage', 'laydate', 'layer'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , laydate = layui.laydate
        , laypage = layui.laypage
        , layer = layui.layer;

    //路径
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPaht = curWwwPath.substring(0, pos);
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);

    var limitcount = 10;
    table.render({
        elem: '#inventorySummary'
        , url: localhostPaht + projectName + "/rest/inventory/inventorySummary"
        , title: "盘点汇总表",
        height: 'full-193'
        , page: false
        , limit: limitcount
        , text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        },
        toolbar: '#',
        defaultToolbar: ['filter', 'print']
        , cols: [[
            // {type: 'checkbox'}
            {field: 'number', title: 'ID', width: 70, sort: true, fixed: 'left'}
            , {field: 'sequence', title: '单据编号', sort: true}
            , {field: 'oprateTimeString', title: '盘点时间', sort: true}
            , {field: 'oldCount', title: '实际库存', sort: true}
            , {field: 'allCount', title: '盘点总数', sort: true}
            , {field: 'thisProfit', title: '盘中占比', sort: true}
            , {field: 'isFinish', title: '状态', sort: true}
            , {title: '操作', toolbar: '#table-useradmin-webuser', align: "center", width: 200}
        ]],
        done: function (res, curr, count) {
            if (res.errorMsg == null) {

            } else {
                layer.msg(res.errorMsg);
            }
            var curnum = $("#curnum").val();
            $('.layui-table-fixed-r').removeClass('layui-hide');//列固定右侧
            laypage.render({
                elem: 'laypage'
                , first: '首页'
                , last: '尾页'
                , groups: 5
                , count: count
                , curr: curnum
                , limit: limitcount
                , layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
                , limits: [10, 15, 20]
                , jump: function (obj, first) {
                    if (!first) {
                        curnum = obj.curr;
                        $("#curnum").val(curnum)
                        limitcount = obj.limit;
                        table.reload('inventorySummary', { //表格的id
                            where: {
                                page: curnum,
                                limit: limitcount
                            }
                        })
                    }
                }
            })
        }
    });

    //监听工具条
    table.on('tool(inventorySummarys)', function (obj) {  //表格的lay-filter="userlists"
        var data = obj.data; //获得当前行所有数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (obj.event === 'info') {
            info(data)
        } else if (obj.event === 'sealup') {
            sealup(data);
        }
    });

    function info(data) {
        layer.open({
            title: "详情",
            type: 2,
            content: localhostPaht + projectName + '/rest/getRoute?type=rfid&url=inventory_info',
            //是否显示右上角关闭图标，默认为1，显示
            area: ['90%', '91%'],
            maxmin: true,
            shade: 0.6,
            anim: 4,
            skin: 'layui-layer-molv',
            resize: true,
            success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                // var body = layer.getChildFrame('body', index);//建立父子联系  //当你试图在当前页获取iframe页的DOM元素时，你可以用此方法。
                // body.find("#id").val(data.id);
                var iframe = window['layui-layer-iframe' + index];
                //调用子页面的全局函数
                iframe.child(data.sequence)
            }
        });
    }


    function sealup(data) {
        var sequence = data.sequence;
        layer.confirm('确定封账此数据', {icon: 3, title: '提示信息'}, function (index) {
            layer.close(index);
            $.post(localhostPaht + projectName + "/rest/inventory/finishInventorysBySequence", {"sequence": sequence},
                function (data) {
                    if (data == true) {
                        table.reload('inventorySummary')
                    }
                }
            )
        })

    }

    exports('inventorySummary', {})
});